﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CUMTConnect
{
	class Boot
	{
		/**
		 * Auto start up when system boot
		 */
		public static bool StartAutomatically()
		{
			if (CUMTConnect.Boot.ShortCutExist())
			{
				Logger.Log.Error("Shortcut Exist!");
				return false;
			}
			try
			{
				IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell();
				IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.WshShortcut)shell.CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ".lnk");
				shortcut.TargetPath = System.Windows.Forms.Application.ExecutablePath;
				shortcut.WorkingDirectory = System.Environment.CurrentDirectory;
				shortcut.WindowStyle = 1;
				shortcut.Description = "用于开机自启的快捷方式";
				shortcut.Save();
				Logger.Log.Info("Create shortcut successful");
				return true;
			}
			catch (Exception e)
			{
				Logger.Log.Error("Create shortcut error : {0}", e.Message);
				return false;
			}
		}

		/**
		 * Check if added to start up list
		 */

		public static bool ShortCutExist()
		{
			return System.IO.File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ".lnk");
		}

		/**
		 * Unlink the start up
		 */
		public static bool DelAutoStart()
		{
			try
			{
				System.IO.File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ".lnk");
				Logger.Log.Error("Remove shortcut successful");
				return true;
			}
			catch (Exception e)
			{
				Logger.Log.Error("Remove shortcut error: {0}", e.Message);
				return false;
			}
		}
	}
}
